<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/26
 * Time: 19:02
 */
class smarty_Adapter implements Yaf_View_Interface{
    public $_smarty;
    protected function getInstance(){
        static $smarty=null;
        if(!$smarty){
            $smarty=new Smarty;
        }
        return $smarty;
    }
    public function __construct($tmplPath = null,$param){
        $this->_smarty = new Smarty;
       if (null !== $tmplPath) {
            $this->setScriptPath($tmplPath);
       }
        foreach($param as $key=>$value){
            $this->_smarty->$key = $value;
        }
    }
    public function render($view_path,$tpl_vars = NULL){
        if(is_array($tpl_vars) && !empty($tpl_vars)){
            $this->_smarty->assign($tpl_vars);
        }
        return  $this->_smarty->fetch($view_path);
    }
    public function display($view_path , $tpl_vars = NULL ){
        if(is_array($tpl_vars) && !empty($tpl_vars)){
            $this->_smarty->assign($tpl_vars);
        }
        $this->_smarty->display($view_path);
    }
    public function assign($spec,$value= null){
        if (is_array($spec)) {
            $this->_smarty->assign($spec);
            return;
        }
        $this->_smarty->assign($spec, $value);
    }
    public function setScriptPath($view_directory){
        if(is_readable($view_directory)){
             $this->_smarty->template_dir = $view_directory;
        }else{
            throw new Exception('Invalid path provided');
        }

    }
    public function getScriptPath(){
        return $this->_smarty->template_dir;
    }
}